package cn.yusei.teamTribe.controller.search;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.service.search.SearchService;
import cn.yusei.teamTribe.util.EmailUtil;
import cn.yusei.teamTribe.util.ResponseUtil;
import cn.yusei.teamTribe.util.StringUtil;

@Controller
public class SearchController {
	@Autowired
	SearchService searchService;
	@Value("${usernameLength}")
	int usernameLength;
	@Value("${phoneLength}")
	int phoneLength;
	
	@RequestMapping(value = "/search/byTeam",method = RequestMethod.POST)
	public void getIdsByTeam(@RequestParam int teamId,PrintWriter out){
		int[] ids;
		try {
			ids = searchService.getIdByTeam(teamId);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<>();
		param.put("ids", ids);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}
	
	@RequestMapping(value = "/search/byTag",method = RequestMethod.POST )
	public void getIdsByTag(@RequestParam int tagId,PrintWriter out){
		int[] ids;
		try {
			ids = searchService.getIdsByTag(tagId);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<>();
		param.put("ids", ids);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}
	
	@RequestMapping(value = "/search/byName",method = RequestMethod.POST)
	public void getIdByName(@RequestParam String username,PrintWriter out){
		try {
			if(StringUtil.haveNullOrEmpty(username) || StringUtil.lengthAbove(username, usernameLength)){
				ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
				return;
			}
			List<Integer> ids = searchService.getIdsByName(username);
			if(ids.size() <= 0){
				ResponseUtil.responseCode(out, ResponseCode.USER_NOT_EXISTS);
				return;
			}
			Map<String, Object> param = new HashMap<>();
			param.put("id", ids);
			ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
			
		}catch (NullPointerException e) {
			ResponseUtil.responseCode(out, ResponseCode.USER_NOT_EXISTS);
		}
	}
	
	@RequestMapping(value = "/search/byPhone",method = RequestMethod.POST)
	public void getIdByPhone(@RequestParam String phone,PrintWriter out){
		try {
			if(!StringUtil.allNumber(phone) || phone.length() != phoneLength){
				ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
				return;
			}
			Integer id = searchService.getIdByPhone(phone);
			if(id == null){
				ResponseUtil.responseCode(out, ResponseCode.USER_NOT_EXISTS);
				return;
			}
			Map<String, Object> param = new HashMap<>();
			param.put("id", id);
			ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
		} catch (NullPointerException e) {
			ResponseUtil.responseCode(out,ResponseCode.USER_NOT_EXISTS);
		}
	}
	
	@RequestMapping(value = "/search/byEmail",method = RequestMethod.POST)
	public void getIdByEmail(@RequestParam String email,PrintWriter out){
        try {
		if(!EmailUtil.isMailAddress(email)){
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		Integer id = searchService.getIdByEmail(email);
		if(id == null){
			ResponseUtil.responseCode(out, ResponseCode.USER_NOT_EXISTS);
			return;
		}
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
				
		}catch (NullPointerException e) {
			ResponseUtil.responseCode(out, ResponseCode.USER_NOT_EXISTS);
		}

	}
	

}
